package com.hotel.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.common.lang.Result;
import com.hotel.entity.House;
import com.hotel.mapper.HouseMapper;
import com.hotel.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2020-10-19
 */
@RestController
@RequestMapping("/house")
public class HouseController {
    @Autowired
    HouseMapper houseMapper;
    @Autowired
    HouseService houseService;

//    分页处理
    @GetMapping("/housePage")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage) {

        Page page = new Page(currentPage, 5);
        IPage pageData = houseService.page(page, new QueryWrapper<House>().orderByDesc("id"));

        return Result.succ(pageData);
    }

    //查询用户
    @GetMapping("/house/{id}")
    public Result detail(@PathVariable(name = "id") Long id) {
        House house = houseService.getById(id);
        Assert.notNull(house, "已删除！");
        return Result.succ(house);
    }

    @PostMapping("/edit")
    public Result edit(@Validated @RequestBody House house) {
        houseService.saveOrUpdate(house);
        return Result.succ("操作成功");
    }

//    修改房间状态
    @PostMapping("/updata/{id}")
    public Result updata(@PathVariable(name = "id")  Long id){
        System.err.println(id);
        houseService.updateById(id);
        return Result.succ("操作成功");
    }
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer id){
        houseService.removeById(id);
    }



    @RequestMapping("/addHouse")
    public Result addHouse(@RequestBody House house){
        System.out.println(house);
        int i = houseMapper.insert(house);
        String str = i >0?"success":"error";
        HashMap<String,String> map = new HashMap<>();
        map.put("info",str);
        return Result.succ(map);
    }
}
